package com.gjw.designpattern.bulider;

/**
 * 建造者模式中的产品类（product）
 *
 * 当创建一个对象需要传入多个参数时会导致可读性比较差。
 * 可以通过建造者模式重构
 */
public class Computer {


    private Cpu cpu;
    private RAM ram;
    private MainBoard mainBoard;

    public Computer() {
    }

    public Computer(Cpu cpu, RAM ram, MainBoard mainBoard) {
        this.cpu = cpu;
        this.ram = ram;
        this.mainBoard = mainBoard;
    }

    @Override
    public String toString() {
        return "Computer{" +
                "cpu=" + cpu +
                ", ram=" + ram +
                ", mainBoard=" + mainBoard +
                '}';
    }

    public Cpu getCpu() {
        return cpu;
    }

    public void setCpu(Cpu cpu) {
        this.cpu = cpu;
    }

    public RAM getRam() {
        return ram;
    }

    public void setRam(RAM ram) {
        this.ram = ram;
    }

    public MainBoard getMainBoard() {
        return mainBoard;
    }

    public void setMainBoard(MainBoard mainBoard) {
        this.mainBoard = mainBoard;
    }
}

//建造者模式重构Computer
class NewComputer{
    private Cpu cpu;
    private RAM ram;
    private MainBoard mainBoard;

    public NewComputer() {
        throw new RuntimeException("弄了个空架子电脑");
    }

    private NewComputer(Builder builder){
        this.cpu = builder.cpu;
        this.ram = builder.ram;
        this.mainBoard = builder.mainBoard;
    }

    public Cpu getCpu() {
        return cpu;
    }

    public void setCpu(Cpu cpu) {
        this.cpu = cpu;
    }

    public RAM getRam() {
        return ram;
    }

    public void setRam(RAM ram) {
        this.ram = ram;
    }

    public MainBoard getMainBoard() {
        return mainBoard;
    }

    public void setMainBoard(MainBoard mainBoard) {
        this.mainBoard = mainBoard;
    }

    public static final class Builder{

        private Cpu cpu;
        private RAM ram;
        private MainBoard mainBoard;

        public Builder() {
        }

        public Builder cpu(Cpu cpu){
            this.cpu = cpu;
            return this;
        }

        public Builder ram(RAM ram){
            this.ram = ram;
            return this;
        }
        public Builder mainBoard(MainBoard mainBoard){
            this.mainBoard = mainBoard;
            return this;
        }

        public NewComputer getComputer(){
            return new NewComputer(this);
        }

    }




}